package com.lz.controller.ly;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.lz.entity.Bank;
import com.lz.entity.Employee;
import com.lz.entity.MortgageSheet;
import com.lz.service.ly.MortgageSheetService;
import com.lz.vo.ly.MortgageSheetVO;
import com.lz.vo.ly.SelectMortgageSheetVO;

import util.BackUtil;
import util.PageUitl;

@Controller
public class MortgageSheetController {

	@Autowired 
	private MortgageSheetService mortgageSheetService;
	
	@RequestMapping("/mortgageGetAll")
	public String mortgageGetAll(Model model,Integer pageNo,MortgageSheet mortgageSheet
			,String start_time,String end_time) {
		if(pageNo == null) {
			pageNo = 1;
		}
		//获取总条数
		Integer count = mortgageSheetService.count(mortgageSheet,start_time,end_time);
		PageUitl<SelectMortgageSheetVO> pageInfo = new PageUitl<>(pageNo, 2, count);
		
		List<SelectMortgageSheetVO> list =mortgageSheetService.getMortgageAll((pageInfo.getPageNo()-1)*2,2,mortgageSheet,start_time,end_time);
		pageInfo.setList(list);
		model.addAttribute("pageInfo", pageInfo);
		model.addAttribute("map", BackUtil.mortgageBack(mortgageSheet, start_time, end_time));
		//获取金融机构
		List<Bank> bankList = mortgageSheetService.getBankList();
		//获取开单人
		List<Employee> employeeList = mortgageSheetService.getEmployeeList();
		
		model.addAttribute("bankList", bankList);
		model.addAttribute("employeeList", employeeList);
		
		return "auditMain";
	}
	@RequestMapping(value="/selMortgage",produces="application/json;charset=utf-8")
	@ResponseBody
	public String selMortgage(Integer id,HttpServletRequest request) {
		MortgageSheet ms = mortgageSheetService.getMortgageById(id);
		if(ms != null) {
			MortgageSheetVO vo = new MortgageSheetVO(ms);
			request.getSession().setAttribute("mortgageSheet", ms);
			return JSON.toJSONString(vo);
		}
		return null;
	}
	
	@RequestMapping(value="/mortgageSheetSH",produces="application/json;charset=utf-8")
	@ResponseBody
	public String mortgageSheetSH(HttpServletRequest request,String opinion,HttpServletResponse response) {
		MortgageSheet ms = (MortgageSheet) request.getSession().getAttribute("mortgageSheet");
		if(ms != null) {
			ms.setMortgageSheetState("审核通过");
		}
		if(opinion == null || "".equals(opinion)) {
			
			System.out.println("*******************************************************"+opinion);
		}
		boolean isTure = mortgageSheetService.updMortgage(ms);
		Cookie[] cookies = request.getCookies();
		for (Cookie cookie : cookies) {
			if(cookie.getName().equals("opinion")) {
				try {
					cookie.setValue(URLEncoder.encode(opinion,"utf-8"));
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
			}else {
				try {
					Cookie cookie1 =new Cookie("opinion", URLEncoder.encode(opinion,"utf-8"));
					cookie1.setMaxAge(60*60*24*30);
					response.addCookie(cookie1);
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
			}
		}
		return JSON.toJSONString(isTure);
	}
	@RequestMapping(value="/mortgageSheetBH",produces="application/json;charset=utf-8")
	@ResponseBody
	public String mortgageSheetBH(HttpServletRequest request,String opinion,HttpServletResponse response) {
		MortgageSheet ms = (MortgageSheet) request.getSession().getAttribute("mortgageSheet");
		if(ms != null) {
			ms.setMortgageSheetState("审核驳回");
		}
		request.getSession().setAttribute("opinion", opinion);
		boolean isTure = mortgageSheetService.updMortgage(ms);
		
		return JSON.toJSONString(isTure);
	}
	
	@RequestMapping(value="/selopinion",produces="application/json;charset=utf-8")
	@ResponseBody
	public String selopinion(HttpServletRequest request) {
		Cookie[] cookies = request.getCookies();
		for (Cookie cookie : cookies) {
			if(cookie.getName().equals("opinion")) {
				try {
					String str = URLDecoder.decode(cookie.getValue(),"utf-8");
					return JSON.toJSONString(str);
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
			}
		}
		return null;
	}

	
}
